/**
 * 
 */
package org.ejc.struts.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.ejc.api.executive.ConventionExecutiveAPIUtil;
import org.ejc.datatype.executive.ExecutiveSearchCriteria;
import org.ejc.session.EJCSession;
import org.ejc.struts.search.SearchRegistrantsForm;

/**
 * @author Brian Boyle
 * @struts.action path="/struts/admin/searchExecutiveAction" scope="request"
 *                parameter="methodToCall" name="searchExecutiveForm"
 *                validate="false"
 *                input="/struts/admin/searchExecutives.jsp"
 * 
 * @struts.action-forward name="searchExecSuccess" path="/struts/admin/searchExecutives.jsp"
 * 
 */
public class SearchExecutiveAction extends DispatchAction {

	private static final int minResultSet = 0;
	private static final int maxResultSet = 10; 
	
	public final ActionForward preSearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		
		ejcSession.setCurrentAction("Search Executives");
		//Hide the navigation initially
		request.setAttribute("hideNavigation", true);
		return mapping.findForward("searchExecSuccess");
	}
	
	public final ActionForward searchExecutives(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		SearchExecutiveForm searchForm = (SearchExecutiveForm)form;
		ExecutiveSearchCriteria searchCriteria = populateSearchCriteria(searchForm);
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		List executives = ConventionExecutiveAPIUtil.getLocalHome().create().searchExecutives(searchCriteria, 0, 5);
		ejcSession.setResultIndex(minResultSet);
		ejcSession.setResultSize(maxResultSet);
		ejcSession.setCriteria(searchCriteria);
		request.setAttribute("listOfExecs", executives);
		request.setAttribute("initialSearch", true);
		//If the size of the resultSet is less than or equal to the max result then do not show the NEXT button
		if(executives.size() < maxResultSet){
			request.setAttribute("noNext", true);
		}
		if(ejcSession.getResultIndex() == 0){
			request.setAttribute("noPrevious", true);
		}
		return mapping.findForward("searchExecSuccess");
	}
	
	public void searchExecutivesPaging(HttpServletRequest request, int maxResult, int minResult) throws Exception
	{
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ExecutiveSearchCriteria searchCriteria = (ExecutiveSearchCriteria) ejcSession.getCriteria();
		
		List executives = ConventionExecutiveAPIUtil.getLocalHome().create().searchExecutives(searchCriteria, minResult, maxResult);
		ejcSession.setResultIndex(minResult);
		ejcSession.setResultSize(maxResult);
		request.setAttribute("listOfExecs", executives);
		//If the size of the resultSet is less than or equal to the max result then do not show the NEXT button
		if(executives.size() < maxResultSet){
			request.setAttribute("noNext", true);
		}
		if(ejcSession.getResultIndex() == 0){
			request.setAttribute("noPrevious", true);
		}
	}
	
	/**
	 * Search the next results in the resultSet
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public final ActionForward searchNext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		int minResult = ejcSession.getResultIndex() + maxResultSet;
		int	maxResult = ejcSession.getResultSize() + maxResultSet;
		searchExecutivesPaging(request, maxResult, minResult);
		return mapping.findForward("searchExecSuccess");
	}
	
	/**
	 * Search the previous results in the resultSet
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public final ActionForward searchPrevious(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		int minResult = ejcSession.getResultIndex() - maxResultSet;
		int	maxResult = ejcSession.getResultSize() - maxResultSet;
		searchExecutivesPaging(request, maxResult, minResult);
		return mapping.findForward("searchExecSuccess");
	}
	
	
	private ExecutiveSearchCriteria populateSearchCriteria(SearchExecutiveForm form)
	{
		ExecutiveSearchCriteria searchCriteria = new ExecutiveSearchCriteria();
		searchCriteria.setFirstName(form.getFirstName());
		searchCriteria.setLastName(form.getLastName());
		searchCriteria.setUserName(form.getUserName());
		searchCriteria.setAdmin(form.getAdmin());
		return searchCriteria;
	}

}
